package com.chenxbook.modules.jbpm4.tool;

import com.chenxbook.modules.boot.constant.CommonConstant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jbpm.api.NewDeployment;
import org.jbpm.api.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipInputStream;

/**
 * <p>className: JbpmRepositoryTool</p>
 * <p>description: 流程资源服务接口工具类</p>
 *
 * @author chenxbook
 * @version 1.0.0
 * @date 2020-05-24 20:43
 */
@Slf4j
@Data
@Component("repositoryTool")
public class JbpmRepositoryTool {

    /**
     * RepositoryService-流程资源服务接口
     * <br> 提供对流程定义的部署、查询、删除等操作。
     */
    @Autowired
    @Qualifier("repositoryService")
    private RepositoryService repositoryService;


    /***
     * 部署流程（文件路径）
     *
     * @author
     * @date 2020/5/24 20:13
     * @param filePath  文件路径
     *
     * @return java.lang.String
     */
    public String publishProcessFromClasspath(String filePath) {
        String deploymentId = this.repositoryService.createDeployment().addResourceFromClasspath(filePath).deploy();
        log.info("-----------------流程发布ID:{}", deploymentId);
        return deploymentId;
    }


    /***
     * 部署流程（zip文件）
     *
     * @author
     * @date 2020/5/24 20:33
     * @param processFile  zip文件
     * @return java.lang.String
     */
    @SuppressWarnings("all")
    public String publishProcessByZip(File processFile) {
        try {
            String fileName = processFile.getName();
            log.info("fileName:{}", fileName);
            NewDeployment deployment = this.repositoryService.createDeployment();
            deployment.setName(fileName);
            deployment.setTimestamp(System.currentTimeMillis());
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(processFile));
            deployment.addResourcesFromZipInputStream(zipInputStream);
            String deploymentId = deployment.deploy();
            log.info("-----------------流程发布ID:{}", deploymentId);
            return CommonConstant.EXECUTE_SUCCESS;
        } catch (Exception e) {
            log.error("部署流程（zip文件）异常", e);
        }
        return CommonConstant.EXECUTE_FAILED;
    }

    /***
     * 通过流程发布ID删除已部署的流程定义
     *
     * @author
     * @date 2020/5/24 22:11
     * @param deploymentId 流程发布ID
     */
    public void deleteDeployment(String deploymentId) {
        repositoryService.deleteDeployment(deploymentId);
        log.info("-----------------流程已删除，发布ID:{}", deploymentId);
    }

    /***
     * 通过流程发布ID删除已部署的流程定义及其所有产生的流程实例
     *
     * @author
     * @date 2020/5/24 22:11
     * @param deploymentId 流程发布ID
     */
    public void deleteDeploymentCascade(String deploymentId) {
        repositoryService.deleteDeploymentCascade(deploymentId);
        log.info("-----------------流程已删除，发布ID:{}", deploymentId);
    }


}